package com.dhcc.sds.standard.service.impl;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.standard.dao.SdsStandSourceMapper;
import com.dhcc.sds.standard.entity.SdsStandSourceEntity;
import com.dhcc.sds.standard.service.ISdsStandSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sds.standard.dao.SdsStandSourceFieldMapper;
import com.dhcc.sds.standard.service.ISdsStandSourceFieldService;
import com.dhcc.sds.standard.entity.SdsStandSourceFieldEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * 数据源输出内容表数据服务层接口
 * 
 * @ClassName: SdsStandSourceFieldServiceImpl
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:09:52
 */
@Service("sdsStandSourceFieldService")
public class SdsStandSourceFieldServiceImpl extends BaseServiceImpl<SdsStandSourceFieldMapper, SdsStandSourceFieldEntity> implements ISdsStandSourceFieldService {

	@Autowired
	private SdsStandSourceMapper sourceMapper;

	@Autowired
	private SdsStandSourceFieldMapper sourceFieldMapper;

	@Autowired
	private ISdsStandSourceService sdsStandSourceService;

	@Override
	public List<SdsStandSourceFieldEntity> page(Page<SdsStandSourceFieldEntity> page, Map<String, Object> map) {
		List<SdsStandSourceFieldEntity> page1 = this.baseMapper.page(page, map);
		for (SdsStandSourceFieldEntity sourceFieldEntity : page1){
			SdsStandSourceEntity sourceEntity = sourceMapper.selectById(sourceFieldEntity.getSourceId());
			sourceFieldEntity.setSourceDesc(sourceEntity.getSourceDesc());
		}
		//return this.baseMapper.page(page, map);
		return page1;
	}
	@Override
	public List<SdsStandSourceFieldEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public JSONObject getSourceFieldMap(Long sourceId) {
		Wrapper<SdsStandSourceFieldEntity> qw = new EntityWrapper<>();
		qw.eq("source_id", sourceId);
		List<SdsStandSourceFieldEntity> sdsStandSourceFieldEntities = sourceFieldMapper.selectList(qw);
		JSONObject sourceFieldMap = new JSONObject();
		for(SdsStandSourceFieldEntity sourceFieldEntity : sdsStandSourceFieldEntities){
			sourceFieldMap.put(String.valueOf(sourceFieldEntity.getId()), sourceFieldEntity.getFieldDesc());
		}
		return sourceFieldMap;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void importEntities(List<List<Object>> all) {
		for (int i = 1; i < all.size(); i++) {
			List<Object> ls = all.get(i);
			String sourceDesc = ls.get(0).toString();
			String code = ls.get(1).toString();
			String desc = ls.get(2).toString();
			String resume = ls.get(3).toString();
			Integer isActive = CommonUtil.equals(ls.get(4),"是")?1:0;
			SdsStandSourceEntity source = sdsStandSourceService.getSourceByDesc(sourceDesc);
			if (StringUtils.isEmpty(source)){
				continue;
			}

			// 判断是否重复
			SdsStandSourceFieldEntity sourceFieldEntity = new SdsStandSourceFieldEntity();
			sourceFieldEntity.setSourceId(source.getId());
			sourceFieldEntity.setFieldCode(code);
			sourceFieldEntity.setSourceDesc(desc);
			sourceFieldEntity = this.baseMapper.selectOne(sourceFieldEntity);
			if (sourceFieldEntity  == null) {
				sourceFieldEntity = new SdsStandSourceFieldEntity();
				sourceFieldEntity.setSourceId(source.getId());
				sourceFieldEntity.setFieldCode(code);
				sourceFieldEntity.setFieldDesc(desc);
				sourceFieldEntity.setIsActive(isActive);
				sourceFieldEntity.setResume(resume);
				sourceFieldEntity.insert();
			}else{
				sourceFieldEntity.setResume(resume);
				sourceFieldEntity.setIsActive(isActive);
				sourceFieldEntity.updateAllColumnById();
			}
		}
	}

	@Override
	public SdsStandSourceFieldEntity selectByDataSourceAndDesc(Long dataSource, String desc) {
		SdsStandSourceFieldEntity sourceFieldEntity = new SdsStandSourceFieldEntity();
		List<SdsStandSourceFieldEntity> sdsStandSourceFieldEntities = this.baseMapper.selectList(new EntityWrapper<SdsStandSourceFieldEntity>()
				.eq("source_id", dataSource)
				.eq("field_desc", desc));
		if (sdsStandSourceFieldEntities != null && sdsStandSourceFieldEntities.size() > 0){
			sourceFieldEntity = sdsStandSourceFieldEntities.get(0);
		}
		return sourceFieldEntity;
	}


}